using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using System.Xml.Serialization;

namespace Galaktika.BI.Extensibility.Dom
{
    [XmlRoot(Namespace=Galaktika.BI.Silverlight.Common.Namespace)]
    public class BrickObject : INamedObject
    {
        public const string TARGET = "target";
        public BrickObject() 
            : this(string.Empty)
        {
        }

        public BrickObject(string name)
            : this(name, name)
        {
        }

        public BrickObject(string name, string title)
        {
            this.Name = name;
            this.Title = title;
            this.NodeIndex = -1;
        }

        public string Title { get; set; }

        public string Name { get; set; }

        private Dictionary<string, BrickObject> m_Properties;
        [XmlIgnore]
        public IDictionary<string, BrickObject> Properties
        {
            get
            {
                if (m_Properties == null)
                {
                    m_Properties = new Dictionary<string, BrickObject>();
                }

                return m_Properties;
            }
        }

        private Dictionary<string, object> m_UserData;
        [XmlIgnore]
        public IDictionary<string, object> UserData
        {
            get
            {
                if (m_UserData == null)
                {
                    m_UserData = new Dictionary<string, object>();
                }

                return m_UserData;
            }
        }

        public int NodeIndex { get; set; }

        public string ToXml()
        {
            return Galaktika.BI.Silverlight.XmlUtility.Obj2XmlStr(this, Galaktika.BI.Silverlight.Common.Namespace);
        }

        public static T FromXml<T>(string xml)
        {
            return Galaktika.BI.Silverlight.XmlUtility.XmlStr2Obj<T>(xml);
        }
    }
}
